#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>

#define _WIN32_WINNT 0x0601  // 启用 Windows Vista+ API
#define __USE_MINGW_ANSI_STDIO 1

#pragma comment(lib, "ws2_32.lib") //link win32 lib

const LPCSTR g_server_ip = "121.41.69.221";
const int g_server_port = 9600;

int checkUserAndPassword(char* username)
{
    for(int i = 0; i < 3; i++)
    {
        std::string _username, 
             _password;
        std::cout << "your name:" << std::endl;
        std::cin >> _username;
    
        std::cout << "your password:" << std::endl;
        std::cin >> _password;
    
        if(!(((_username == "hankaizheng") || (_username == "zhangchencan") || (_username == "zhouzhou"))\
            && (_password == "123")))
        {
            if(i != 2)
            {
                std::cout << "login in failed, please try it again:" << std::endl;
                Sleep(3000);
                system("cls");
            }
            else
            {
                std::cout << "you have try out max times, see you !" << std::endl;
                Sleep(3000);
                return 1;
            }

        }
        else
        {
            username = _username.data();
            break;
        }
    }
    return 0;
}

int main()
{
    //initialize windows socket
    WSADATA wsa_data;
    if (WSAStartup(MAKEWORD(2, 2), &wsa_data) != 0) 
    {
        std::cerr << "WSA Startup Failed: " << WSAGetLastError() << std::endl;
        return 1;
    }

    //create socket
    SOCKET client_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (client_socket == INVALID_SOCKET) 
    {
        std::cerr << "Socket Creation Failed: " << WSAGetLastError() << std::endl;
        WSACleanup();
        return 1;
    }

    // configure server ip and port
    sockaddr_in server_address;
    server_address.sin_family = AF_INET;
    server_address.sin_port = htons(g_server_port);  // server port
    inet_pton(AF_INET, g_server_ip, &server_address.sin_addr);  //server ip

    // connect to server
    if (connect(client_socket, (sockaddr*)&server_address, sizeof(server_address)) == SOCKET_ERROR) 
    {
        std::cerr << "Connection Failed: " << WSAGetLastError() << std::endl;
        closesocket(client_socket);
        WSACleanup();
        return 1;
    }

    std::cout << "Connected to server!" << std::endl;

    //check name and password
    char *user_name, *send_data;
    if(checkUserAndPassword(user_name))
    {
        return 1;
    }

    // send username to authenticated
    if (send(client_socket, user_name, strlen(user_name), 0) == SOCKET_ERROR) 
    {
        std::cerr << "Send Failed: " << WSAGetLastError() << std::endl;
    }

    //send data
    while(1)
    {
        std::cout << user_name << ":" << "say something :" << std::endl;
        std::cin >> send_data;
    
        if (send(client_socket, send_data, strlen(send_data), 0) == SOCKET_ERROR) 
        {
            std::cerr << "Send Failed: " << WSAGetLastError() << std::endl;
        }
    }

    // receive data
    char recvBuffer[1024];
    int bytesReceived = recv(client_socket, recvBuffer, sizeof(recvBuffer), 0);
    if (bytesReceived > 0) 
    {
        recvBuffer[bytesReceived] = '\0';  // 添加字符串终止符
        std::cout << "Received: " << recvBuffer << std::endl;
    } 
    else if (bytesReceived == 0) 
    {
        std::cout << "Connection Closed By Server." << std::endl;
    }
     else 
    {
        std::cerr << "Receive Failed: " << WSAGetLastError() << std::endl;
    }

    // close connection
    closesocket(client_socket);
    WSACleanup();
    return 0;

}